//
//  xsBossIPBackgroundTableViewCell.m
//  digital_robot_demo
//
//  Created by xskj on 2024/9/2.
//

#import "xsBossIPBackgroundTableViewCell.h"


@interface xsBossIPBackgroundTableViewCell()


@property (nonatomic,strong) UIImageView        *iconimageView;
@property (nonatomic,strong) UILabel            *titleLabel;
@property (nonatomic,strong) UILabel            *timeLabel;
@property (nonatomic,strong) UIButton          * xsUseButton;
@property (nonatomic,strong) UIView          * xsUseShadowView;// 渐变
@property (nonatomic,strong) UILabel            *bottomLineLabel;
@property (nonatomic,strong) UIButton          * xsDeleteButton;

@end

@implementation xsBossIPBackgroundTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  withIsMe:(BOOL)isMe{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.isMeModel = isMe;
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    
    [self.contentView addSubview:self.xsUseShadowView];
    [self.contentView addSubview:self.iconimageView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.xsUseButton];
    [self.contentView addSubview:self.bottomLineLabel];
//    if(self.isMeModel){
//
//    }
    [self.contentView addSubview:self.xsDeleteButton];
    CGFloat margin15 = 15.0;
    CGFloat margin10 = 10.0;
    [self.iconimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(margin15);
        make.centerY.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(45, 45));
    }];
    
    CGFloat xsUseShadowViewW = 110;
    CGFloat xsUseShadowViewX = kScreenWidth-xsUseShadowViewW-margin15;
    CGFloat xsUseShadowViewH = 30;
    CGFloat xsUseShadowViewY = (self.height - xsUseShadowViewH)*0.5;
    self.xsUseShadowView.frame = CGRectMake(xsUseShadowViewX, xsUseShadowViewY, xsUseShadowViewW, xsUseShadowViewH);
    self.xsUseShadowView.layer.cornerRadius = 6.0;
    self.xsUseShadowView.layer.masksToBounds = YES;
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, xsUseShadowViewW, xsUseShadowViewH);
    gradientLayer.startPoint = CGPointMake(0,0.5);
    gradientLayer.endPoint = CGPointMake(1.0,0.5);
    //gradientLayer.locations = @[@(0.5), @(0.0)];
    gradientLayer.colors = @[(id)COLOR_HEX(0x98A3DC).CGColor,
                             (id)COLOR_HEX(0xE9DBBE).CGColor];
    [self.xsUseShadowView.layer addSublayer:gradientLayer];
    
    
    
    CGFloat xsUseButtonW = 110-3.0;
    CGFloat xsUseButtonX = CGRectGetMinX(self.xsUseShadowView.frame)+1.5;
    CGFloat xsUseButtonH = xsUseShadowViewH-3.0;
    CGFloat xsUseButtonY = (self.height - xsUseButtonH)*0.5;
    self.xsUseButton.frame = CGRectMake(xsUseButtonX, xsUseButtonY, xsUseButtonW, xsUseButtonH);
    self.xsUseButton.layer.cornerRadius = 6.0;
    self.xsUseButton.layer.masksToBounds = YES;
    self.xsUseButton.backgroundColor = UIColor.blackColor;
    
//    [self.xsUseButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.contentView.mas_right).offset(-margin15);
//        make.centerY.equalTo(self.contentView);
//        make.size.mas_equalTo(CGSizeMake(110, 30));
//    }];
    self.xsUseButton.layer.cornerRadius = 6.0;
    self.xsUseButton.layer.masksToBounds = YES;
    //        _titleLabel.layer.shadowColor   = COLOR_RGB(228, 228, 228, 1).CGColor;
    //        _titleLabel.layer.shadowOffset  = CGSizeMake(0,0.5);
    //        _titleLabel.layer.shadowOpacity = 1;
    //        _titleLabel.layer.shadowRadius  = 0;
    
    
    
    
    
    if(self.isMeModel){
        
        CGFloat rightMargin = kScreenW - CGRectGetMinX(self.xsUseShadowView.frame);
        [self.xsDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.xsUseButton.mas_centerY);
//            make.right.equalTo(self.xsUseButton.mas_left).offset(-margin10);
            make.right.equalTo(self.contentView.mas_right).offset(-rightMargin-10);
            make.size.mas_equalTo(CGSizeMake(30, 40));
        }];

        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconimageView.mas_top);
            make.left.equalTo(self.iconimageView.mas_right).offset(margin10);
            make.right.equalTo(self.xsDeleteButton.mas_left).offset(-margin10);
        }];
    }
    else{
        CGFloat rightMargin = kScreenW - CGRectGetMinX(self.xsUseShadowView.frame);
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconimageView.mas_top);
            make.left.equalTo(self.iconimageView.mas_right).offset(margin10);
//            make.right.equalTo(self.xsUseButton.mas_left).offset(-margin10);
            make.right.equalTo(self.contentView.mas_right).offset(-rightMargin-10);
        }];
    }
    
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.iconimageView.mas_bottom);
    }];
    
    [self.bottomLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconimageView.mas_left);
        make.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(1);
    }];
    
    
}

- (void)layoutSubviews{
    NSLog(@"cell----layoutSubviews");
    
}

- (void)setModel:(xsBossIPBackgroundTableViewCellModel *)model{
    
    _model = model;
    
    //self.iconimageView.image = [UIImage imageNamed:model.thumbnailUrl];
    
    if(!kStringIsEmpty(model.title)){
        self.titleLabel.text = model.title;
    }
    else{
        self.titleLabel.text = @"";
    }
    if(!kStringIsEmpty(model.duration)){
        self.timeLabel.text = model.duration;
    }
    else{
        self.timeLabel.text = @"";
    }
    
    if(!kStringIsEmpty(model.thumbnailUrl)){
        XSWeakSelf;
        NSData *urlImageData =  [[SDImageCache sharedImageCache]diskImageDataForKey:model.thumbnailUrl];
        if(urlImageData){
            weakSelf.iconimageView.image = [UIImage imageWithData:urlImageData];
        }
        else{
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.thumbnailUrl] options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                
            } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                    if (image) {
                        [[SDImageCache sharedImageCache] storeImageDataToDisk:data forKey:model.thumbnailUrl];
                        
                        // 图片下载成功，将其设置到UIImageView中
                        dispatch_async(dispatch_get_main_queue(), ^{
                            weakSelf.iconimageView.image = image;
                        });
                    } else {
                        // 处理错误
                        XSLogMethod
                        NSLog(@"Error downloading image: %@", error);
                    }
            }];
        }
    }
    else{
        
        self.iconimageView.image = [UIImage imageNamed:@"bossIPBgMusicImageTb"];
    }
    
    
    if(!model.isUse){
        
//        self.xsUseButton.backgroundColor = COLOR_HEX(0x343543);
//        [self.xsUseButton setTitle:kLocalizedString(@"使用") forState:UIControlStateNormal];
        
        
        
        self.iconimageView.layer.borderWidth = 0.0;
        self.iconimageView.layer.borderColor = UIColor.clearColor.CGColor;
        self.iconimageView.layer.cornerRadius = 6.0;
        self.iconimageView.layer.masksToBounds = YES;
        //        _titleLabel.layer.shadowColor   = COLOR_RGB(228, 228, 228, 1).CGColor;
        //        _titleLabel.layer.shadowOffset  = CGSizeMake(0,0.5);
        //        _titleLabel.layer.shadowOpacity = 1;
        //        _titleLabel.layer.shadowRadius  = 0;
        
    }
    else{
        
//        self.xsUseButton.backgroundColor = COLOR_Blue;
//        [self.xsUseButton setTitle:kLocalizedString(@"使用中") forState:UIControlStateNormal];
        
        self.iconimageView.layer.borderWidth = 2.0;
        self.iconimageView.layer.borderColor = COLOR_Blue.CGColor;
        self.iconimageView.layer.cornerRadius = 6.0;
        self.iconimageView.layer.masksToBounds = YES;
        
    }
    //self.xsUseButton.selected = model.isUse;
}


- (UIImageView *)iconimageView{
    if (!_iconimageView) {
        _iconimageView = [[UIImageView alloc] init];
        _iconimageView.layer.masksToBounds = YES;
        _iconimageView.layer.cornerRadius  = 6.f;
        ////        _imageView.layer.borderWidth = 1.0f;
        ////        _imageView.layer.borderColor = [HexRGB(0x444444, 1.0f) CGColor];
        ////        [_imageView setTitleColor:HexRGB(0x444444, 1.0f) forState:UIControlStateNormal];
        ////        [_imageView.titleLabel setFont:FontMedium(14.f)];
        ////        [_imageView addTarget:self action:@selector(otherAction) forControlEvents:UIControlEventTouchUpInside];
        //        [_imageView setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        //        [_imageView setImage:[UIImage imageNamed:@"editorFontSizeSelect"] forState:UIControlStateSelected];
        //        _iconimageView.image = [UIImage imageNamed:@"BottomSelectCellViewSelectImage"];
        _iconimageView.backgroundColor = UIColor.clearColor;//COLOR_DanGrayF8;
        //        _iconimageView.hidden = NO;
    }
    return _iconimageView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:16];//FontMedium(14);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.numberOfLines = 1;
        _titleLabel.textColor = UIColor.whiteColor;
        //        _titleLabel.text = @"";
        _titleLabel.backgroundColor     = UIColor.clearColor;
        //        _titleLabel.layer.shadowColor   = COLOR_RGB(228, 228, 228, 1).CGColor;
        //        _titleLabel.layer.shadowOffset  = CGSizeMake(0,0.5);
        //        _titleLabel.layer.shadowOpacity = 1;
        //        _titleLabel.layer.shadowRadius  = 0;
    }
    return _titleLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];//Font(12);
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.numberOfLines = 1;
        //        _timeLabel.text = @"";
        _timeLabel.backgroundColor     = UIColor.clearColor;
        //        _timeLabel.layer.shadowColor   = COLOR_RGB(228, 228, 228, 1).CGColor;
        //        _timeLabel.layer.shadowOffset  = CGSizeMake(0,0.5);
        //        _timeLabel.layer.shadowOpacity = 1;
        //        _timeLabel.layer.shadowRadius  = 0;
        _timeLabel.textColor = UIColor.grayColor;//COLOR_TextGray2;
        
    }
    return _timeLabel;
}


- (UILabel *)bottomLineLabel{
    if (!_bottomLineLabel) {
        _bottomLineLabel = [[UILabel alloc] init];
        _bottomLineLabel.backgroundColor     = UIColor.lightGrayColor;//COLOR_LineGray3;
        _bottomLineLabel.alpha = 0.12;
    }
    return _bottomLineLabel;
}

- (UIButton *)xsUseButton{
    
    if (!_xsUseButton) {
        _xsUseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsUseButton.backgroundColor = UIColor.clearColor;
        _xsUseButton.titleLabel.font = Font(16);//Font(12);
        [_xsUseButton setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
        [_xsUseButton addTarget:self action:@selector(xsUseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsUseButton setImage:[UIImage imageNamed:@"bossIPBackgroundMusicSoundImg"] forState:UIControlStateNormal];
        //[_xsUseButton setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
        [_xsUseButton setTitle:[NSString stringWithFormat:@" %@",kLocalizedString(@"设置音量")] forState:UIControlStateNormal];
        
        //[_xsUseButton setImageEdgeInsets:UIEdgeInsetsMake(4, 4, 4, 4)];
        
//        [_xsUseButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
//        //[_xsUseButton setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
//        [_xsUseButton setTitle:kLocalizedString(@"使用") forState:UIControlStateNormal];
        //[_xsUseButton setTitle:kLocalizedString(@"使用中") forState:UIControlStateSelected];
        
        _xsUseButton.hidden = NO;
        _xsUseButton.backgroundColor = UIColor.clearColor;//COLOR_HEX(0x343543);
        //_xsUseButton.enabled = NO;
        
    }
    return _xsUseButton;
    
}


- (void)xsUseButtonAction:(UIButton *)sender{
    
    
    NSLog(@"xsUseButtonAction");
    
    sender.selected = !sender.selected;
    
    if(self.cellUseButtonCallBackBlock){
        self.cellUseButtonCallBackBlock(_model);
    }
    
}

- (UIButton *)xsDeleteButton{
    
    if (!_xsDeleteButton) {
        _xsDeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsDeleteButton.backgroundColor = UIColor.clearColor;
        [_xsDeleteButton addTarget:self action:@selector(xsDeleteButtonnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsDeleteButton setImage:[UIImage imageNamed:@"xsDeleteButtonImg"] forState:UIControlStateNormal];
        _xsDeleteButton.hidden = NO;
        _xsDeleteButton.backgroundColor = UIColor.clearColor;
    }
    return _xsDeleteButton;
    
}

- (void)xsDeleteButtonnAction:(UIButton *)sender{
    
    
    NSLog(@"xsDeleteButtonnAction");
    if(self.cellDeleteCallBackBlock){
        self.cellDeleteCallBackBlock(_model);
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}




- (UIView *)xsUseShadowView {
    if (!_xsUseShadowView) {
        _xsUseShadowView = [[UIView alloc] init];
    }
    return _xsUseShadowView;
}

@end
